package TUILayer;
import java.util.Scanner;
import ControlLayer.DVDCtr;
import java.util.*;
/**
 * User interface to the DVD controller
 * @author(Group1:Georgi,Roland and Mindaugas)
 * @version(2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 * (KBH): 2008.10.24: dvdmenu changed to If                   
 */

public class DvdUI
{
    private DVDCtr dvdCtr;//DVD UI must know the DVD controller 
    /**
     * Constructior for DVDUI.
     */
    public DvdUI()
    {
        dvdCtr = new DVDCtr();
    }
    /**
     * Starts the program.
     */
    public void start()
    {
         printWelcomeMessage();
        dvdMenu();
    }
    /**
     * The DVD menu.
     */
    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choise =   writeDVDMenu();
            if (choise == 1)
            {
                findDVD();
            }
            else{if(choise == 2){
                   findCopy();
                }
                else{if(choise == 3){
                   createDVD();
                }
                else{
                    if(choise == 4){
                        createCopy();
                    }
                    else{
                        if(choise == 5){
                             deleteDVD();
                        }
                        else{if(choise == 6){
                            deleteCopy();
                                 }
                        else{
                           if(choise == 7){
                             updateDVD();
                              }
                          else{
                              if(choise == 8){
                                   updateCopy();
                                 }
                            else{
                                if(choise == 9){
                                   listAllDvds();
                                      }
                              

                                else{
                                    exit = true;
                            }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
        }//end while
      }
     }
   }
   }
  }
 
    /**
     * Print out the DVDmenu.
     */
    private void printWelcomeMessage()
    {
             System.out.println("\f *** DVD Menu ***");
            System.out.println(" (1) Show DVD information");
            System.out.println(" (2) Show Copy information");
            System.out.println(" (3) Create DVD ");
            System.out.println(" (4) Create Copy ");
            System.out.println(" (5) Delete DVD");
            System.out.println(" (6) Delete Copy");
            System.out.println(" (7) Update DVD");
            System.out.println(" (8) Update Copy");
            System.out.println(" (9) Show current DVD list");
            System.out.println(" (10) Return");
            System.out.print("\n Make your choise: ");
            
    }
    
    /**
     * Writes the DVDMenu.
     */
    private int writeDVDMenu()
    {
          
            while(true)
            {
                try{
                    Scanner keyboard = new Scanner(System.in);
            int choise = keyboard.nextInt();
            return choise;
        }
        
        catch(InputMismatchException e)
        {
            System.out.println("Please use right input and try agian");
        }
    }
    }
    
    /**
     * Allows the uset to set the DVDNO.
     */
     private long inputDVDNo()
    {   
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
  
        System.out.println("Write DVD Number:  ");
         Scanner keyboard = new Scanner(System.in);
        try{      
            id = keyboard.nextLong();
            ok = true;
        }
        catch (Exception e)
        {
            System.out.println("Enter a valid DVD Number!");
          
        }
        }
        return id;
    }
        
    /**
     * Allows the user to set the Title.
     */
    public String inputTitle()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD title: " );
        String title = keyboard.nextLine();
        return title;
    }
    
    /**
     * Allows the user to set the copyNO.
     */
    private long inputCopyNo()
    {   
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Write Copy Number:  ");
        try{      
            id = keyboard.nextLong();
            ok = true;
        }
        catch (Exception e)
        {
            System.out.println("Enter a valid Copy Number!");
            
        }
        }
        return id;
    }
    
    public String inputArtist()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Write Artist:  ");
        String artist = keyboard.nextLine();
        return artist;
    }
    
    /**
     * Allows the user to set the publication date
     */
    public String inputPublicationDate()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Write Publication Date:  ");
        String publicationDate = keyboard.nextLine();
        return publicationDate;
    }
    
    /**
     *allows the uset to Set the DVDs id.
     */
    public long inputDVDID()
    {   
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Write DVD ID:  ");
        try{      
            id = keyboard.nextLong();
            ok = true;
        }
        catch (Exception e)
        {
            System.out.println("Enter a valid DVD ID!");
            
        }
        }
        return id;
    }
 
  /**
   * searches for a DVD.
   */
    private void findDVD()
    { 
        long id=inputDVDID();
        
        if(!dvdCtr.dvdExist(id))
        {
            System.out.println("Dvd doesn't exist,please try again");
            return;
        }
       
      try{
     
         System.out.println("Title:  " + dvdCtr.getDVDTitle(id));
         System.out.println("Artist:  " + dvdCtr.getDVDArtist(id)); 
         System.out.println("Publication Date:  " + dvdCtr.getDVDPublicationDate(id));
      
         }
    
       catch(Exception e)
        {
         System.out.println("Error occured,please try again!");
        }
   }
 
   /**
    * Creates a DVD
    */
    
    private void createDVD()
    {
        long dvdID=inputDVDID();
        
        if(dvdCtr.dvdExist(dvdID))
        {
            System.out.println("Dvd ID already exist,please try again");
            return;
        }
        
        String title=inputTitle();
        String artist=inputArtist();
        String publicationDate=inputPublicationDate();
        
        try{
        dvdCtr.createDVD(dvdID, title, artist, publicationDate);
          }
    
        catch(Exception e){
        System.out.println("Error occured while creating the DVD, please try again");
        }
        
        printWelcomeMessage();
    }
    /**
     * updates the dvd.
     */
    private void updateDVD()
    {
        long id=inputDVDID();
        
        if(!dvdCtr.dvdExist(id))
        {
            System.out.println("Dvd doesn't exist,please try again");
            return;
        }
        
        String Title=inputTitle();
        String Artist=inputArtist();
        String PublicationDate=inputPublicationDate();
        try{
            dvdCtr.updateDVD(id, Title, Artist, PublicationDate);
         }
         catch(Exception e){
             System.out.println("Error occured while updating the DVD, illegal variables");
             
            }
            
            printWelcomeMessage();
    }
    
    /**
     * Allows the uset to set the copy serial number.
     */
   public long inputCopySerialNumber()
    {   
        boolean ok = false; 
        long id = 0;
        while(!ok)
        {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Write Copy Serial Number:  ");
        try{      
            id = keyboard.nextLong();
            ok = true;
        }
        catch (Exception e)
        {
            System.out.println("Please enter a valid Serial Number!");
       
        }
        }
        return id;
    }
    
    /**
     *Allows the uset to Set the purchase date.
     */
    public String inputPurchaseDate()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Write Purchase Date:  ");
        String purchaseDate = keyboard.nextLine();
        return purchaseDate;
    }
    
    /**
     *Allows the uset to Set the purhcase price.
     */
    public int inputPurchasePrice()
    {   
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Write Purchase Price:  ");
        try{      
            id = keyboard.nextInt();
            ok = true;
        }
        catch (Exception e)
        {
            System.out.println("Please enter a valid amount!");
           
        }
        }
        return id;
    }
    /**
     * Creates a copy.
     */
    private void createCopy()
    {    
        long DVDID=inputDVDID();
        if(!dvdCtr.dvdExist(DVDID))
        {
          System.out.println("DVD doesn't exist,please try again");
          return;
        }
        
        long CopySerialNumber=inputCopySerialNumber();
        if(dvdCtr.copyExist(DVDID,CopySerialNumber))
          {
          System.out.println("Copy ID already exist,please try again");
          return;
          }
        String PurchaseDate=inputPurchaseDate();
        int PurchasePrice=inputPurchasePrice();
       
        try{
        dvdCtr.createCopy(DVDID, CopySerialNumber, PurchaseDate, PurchasePrice);
        }
        
       catch(Exception e){
        
         System.out.println("Error occured while creating Copy, please try again!");
        }
        
        printWelcomeMessage();
    }
    
    /**
     * deletes a copy.
     */
    private void deleteCopy()
    {
        long DVDID=inputDVDID();
        
        if(!dvdCtr.dvdExist(DVDID))
        {
          System.out.println("DVD doesn't exist,please try again");
          return;
        }
        
        long CopySerialNumber=inputCopySerialNumber();
        if(!dvdCtr.copyExist(DVDID,CopySerialNumber))
          {
          System.out.println("Copy doesn't exist,please try again");
          return;
          }
          
      
        dvdCtr.deleteCopy(DVDID, CopySerialNumber);
        
        
   
       
    }
    
    /**
     * Updates Copy.
     */
    private void updateCopy()
    {    
        long DVDID=inputDVDID();
        
        if(!dvdCtr.dvdExist(DVDID))
        {
          System.out.println("DVD doesn't exist,please try again");
          return;
        }
        
        long CopySerialNumber=inputCopySerialNumber();
        
        if(!dvdCtr.copyExist(DVDID,CopySerialNumber))
        {
          System.out.println("Copy doesn't exist,please try again");
          return;
        }
        
        String PurchaseDate=inputPurchaseDate();
        int PurchasePrice=inputPurchasePrice();
        
        try{
             dvdCtr.updateCopy(DVDID, CopySerialNumber, PurchaseDate, PurchasePrice);
         }
         
        catch(Exception e){
         System.out.println("Error occured while updating Copy, please try again!");
        }
        
        printWelcomeMessage();
    }
    /**
     * Deletes a DVD.
     */
    private void deleteDVD()
    {
       long id=inputDVDID();
        
        if(!dvdCtr.dvdExist(id))
        {
            System.out.println("Dvd does not exist");
            return;
        }
        
         
          dvdCtr.deleteDVD(id);
         
  
    }
    /**
     * Searches a copy.
     */
    private void findCopy()
    {
       printWelcomeMessage();
       System.out.println();
       long id = inputDVDID();
       
       if(!dvdCtr.dvdExist(id))
       {
          System.out.println("DVD doesn't exist,please try again");
          return;
        }
        
       long serialNumber = inputCopySerialNumber();
       
        if(!dvdCtr.copyExist(id,serialNumber))
        {
          System.out.println("Copy doesn't exist,please try again");
          return;
        }
       
       try{
       System.out.println("Purchase Date:  " +dvdCtr.getCopyPurchaseDate(id, serialNumber));
       System.out.println("Purchase Price:  " +dvdCtr.getCopyPurchasePrice(id, serialNumber));
        }
        
        catch(Exception e){
             System.out.println("Error occured,please try again!");
            }
    }
    
    /**
     * Returns all the existing DVDs
     */

    private void listAllDvds()
    {
        try{
        ArrayList<Long> IDs = dvdCtr.getAllIDs();
        ArrayList<String> titles = dvdCtr.getAllTitles();
        ArrayList<String> artists = dvdCtr.getAllArtists();
        ArrayList<String> publicationDates = dvdCtr.getPublicationDates();
        if(IDs.isEmpty()){
            
        System.out.println("The list of DVD's is empty.");

    }else for(int index=0; index<IDs.size(); index++){
            System.out.println((index+1)+".ID: "+ IDs.get(index)+"   Title: " + titles.get(index) +"   "+ " Artist: " + artists.get(index)+"   "+  " Publication date: " + publicationDates.get(index)  );
        }
    }
    
    catch(Exception e){
        System.out.println("Error occured while listing all DVD's, please try again.");
    }
}
        
    
    
    
    
}